package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 29143
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    /**
     * 根据名称分页查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {

        //判断参数是否为null
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //构建分页参数
        Page page=new Page(dto.getPage(),dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //判断名称是否为空
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //构建查询排序条件(根据顺序编号升序排序)
        queryWrapper.orderByAsc(AdChannel::getOrd);

        IPage result = page(page,queryWrapper);

        //封装返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    /**
     * 新增频道列表
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult add(AdChannel adChannel) {

        //判断参数是否为空
        if (StringUtils.isBlank(adChannel.getName())&&adChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //构建查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //与数据库已存在的频道名称作比较
        int count = this.count(queryWrapper.eq(AdChannel::getName, adChannel.getName()));
        //判断返回结果是否大于0
        if (count>0) {
            //当返回结果大于0时,频道已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
        }
        //判断参数的频道名称长度是否大于10
        if (adChannel.getName().length()>10) {
            //当参数的频道名称大于10,返回错误结果
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能大于10");
        }

        adChannel.setCreatedTime(new Date());
        //执行新增方法
        this.save(adChannel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {

        //判断参数是否为空
        if (adChannel==null&&adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据参数id查询频道信息
        AdChannel channel = this.getById(adChannel.getId());
        //判断查询的频道信息是否为空
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //构建查询条件对象
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //判断频道名称是否相同
        if (StringUtils.isNotBlank(adChannel.getName())&&!adChannel.getName().equals(channel.getName())) {
            int count = this.count(queryWrapper.eq(AdChannel::getName, adChannel.getName()));
            //判断查询的结果是否大于0
            if (count>0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
            }
        }
        adChannel.setCreatedTime(new Date());
        //执行修改方法
        this.updateById(adChannel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteBuId(Integer id) {
        //判断参数id是否为空
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询频道列表
        AdChannel channel = this.getById(id);
        //查询的结果是否为空
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断频道是否在启用状态下
        if (channel.getStatus()){
            //频道在启用状态下不能被删除
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        //测试全局异常
//        int i=1/0;
        //执行删除方法
        this.removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }
}
